package com.pool.store;

import java.util.List;

import com.pool.auth.Auth;
import com.pool.common.Lists;
import com.pool.common.Record;
import com.pool.net.SpreadsheetsConnector;
import com.pool.store.gdata.RecordGDataFactory;

/**
 * Provides an interface with Google Spreadsheets API for storing/retrieving {@link Record}s.
 * 
 * @author claveld@gmail.com (David Clavel)
 */
public class SpreadsheetsRecordStore extends AbstractSpreadsheetsStore<Record> implements Store<Record> {

  private static final String RESULTS_2010_TABLE_URL =
    "http://spreadsheets.google.com/feeds/tg91qh8FwCs_tWTylybERFg/records/1";
  
  public SpreadsheetsRecordStore() {
    this(Auth.get(), SpreadsheetsConnector.get());
  }
  
  public SpreadsheetsRecordStore(Auth auth, SpreadsheetsConnector connector) {
    super(auth, connector);
  }
  
  public boolean add(Record record) {
    String xml =
      "<entry xmlns=\"http://www.w3.org/2005/Atom\"" +
      "       xmlns:gs=\"http://schemas.google.com/spreadsheets/2006\">" +
      "  <title>New shoot</title>" +
      "  <gs:field name='Timestamp'>" + record.timestamp + "</gs:field>" +
      "  <gs:field name='Player'>" + record.player + "</gs:field>" +
      "  <gs:field name='Shoot'>" + record.shootId + "</gs:field>" +
      "  <gs:field name='Remaining balls'>" + record.remainingBalls + "</gs:field>" +
      "</entry>";
    return sendCreateRequest(RESULTS_2010_TABLE_URL, xml);
  }

  public List<Record> getAll() {
    List<Record> records = Lists.newArrayList();
    sendRetrieveRequest(RESULTS_2010_TABLE_URL, new RecordGDataFactory(), records);
    return records;
  }

  @Deprecated
  public boolean delete(Record e) { return false; }
  @Deprecated
  public Record get(String id) { return null; }
  @Deprecated
  public int getCount() { return 0; }
  @Deprecated
  public boolean update(Record e) { return false; }
  @Deprecated
  public boolean deleteAll() { return false; }
}
